//===COMMENT===

package bubble3d.animation {

	public class ActionLibrary {

		import bubble3d.animation.Animation;
		import bubble3d.interaction.CycleNotification;

		private static var _instance:ActionLibrary = null;

		private var actionObjects:Array;

		public static function instance():ActionLibrary {
			if (ActionLibrary._instance == null) {
				ActionLibrary._instance = new ActionLibrary();
			}
			return ActionLibrary._instance;
		}

		public function ActionLibrary() {
			trace("[ActionLibrary] created");
			actionObjects = new Array();
		}

		public function reset():void {
			/*
			for(var x in actionObjects) {
				for(var y in actionObjects[x]) {
					for(var i:Number = 0; i<actionObjects[x][y].getElements().length; i++) {
						delete actionObjects[x][y].getElements()[i];
					}
					delete actionObjects[x][y];
				}
				delete actionObjects[x];
			}
			*/
			actionObjects = new Array();
		}

		public function getActionsForObject(modelName:String):Array {
			return (actionObjects[modelName]);
		}

		public function addAnimationXML(modelName:String, animation:XMLList):void {

			trace("addAnimationXML for " + modelName);

			var actionsXML:XMLList = animation.elements("Action");

			for (var i:Number = 0; i < actionsXML.length(); i++) {

				var id:String = actionsXML[i].attribute("id");

				var a:Action = addAction(id, modelName);

				var r:XMLList = actionsXML[i].elements("Rotation");

				for (var j:Number = 0; j < r.length(); j++) {
					a.addElement(Rotation, r[j].attribute("object"), r[j].attribute("axis"), r[j].attribute("amount"), r[j].attribute("duration"));
				}

				var m:XMLList = actionsXML[i].elements("Motion");

				for (var k:Number = 0; k < m.length(); k++) {
					a.addElement(Motion, m[k].attribute("object"), m[k].attribute("axis"), m[k].attribute("amount"), m[k].attribute("duration"));
				}

			}

		}

		public function addAction(id:String, modelName:String):Action {

			if (!actionObjects[modelName]) {
				actionObjects[modelName] = new Array();
			}

			actionObjects[modelName][id] = new Action(id);

			return (actionObjects[modelName][id]);

		}

	}
}
